package com.xsf.pay.common.utils;

import cn.hutool.core.util.StrUtil;
import com.xsf.pay.common.domain.CodeMsg;
import com.xsf.pay.common.exception.BusinessRunTimeException;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import org.hibernate.validator.HibernateValidator;
import org.springframework.util.ObjectUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description 校验工具类
 * @Author xujianfeng
 * @DATE 2023/7/7 10:39
 */
public class ValidationUtils {

    /**
     * 手机号正则
     */
    private static final Pattern PHONE_PATTEN = Pattern.compile("[1]([3-9])[0-9]{9}$");
    private static Validator validator = Validation.byProvider(HibernateValidator.class).configure().failFast(false).buildValidatorFactory().getValidator();
    private static final Pattern URL_PATTEN = Pattern.compile("^(http|https)://[a-z0-9]+([\\-\\.]{1}[a-z0-9]+)*\\.[a-z]{2,5}(:[0-9]{1,5})?(\\/.*)?$");

    /**
     * 手动校验参数  功能和@valid一样
     *
     * @param object
     */
    public static void validate(Object object) {
        Set<ConstraintViolation<Object>> validateResult = validator.validate(object);
        if (!validateResult.isEmpty()) {
            validateResult.forEach(res -> {
                throw new BusinessRunTimeException(CodeMsg.FAIL.getCode(), res.getMessage());
            });
        }
    }

    /**
     * 手动触发校验参数  功能和@valid一样
     *
     * @param object
     * @param group
     */
    public static void validate(Object object, Class<?> group) {
        Set<ConstraintViolation<Object>> validateResult = ObjectUtils.isEmpty(group) ?
                validator.validate(object) : validator.validate(object, group);
        if (!validateResult.isEmpty()) {
            validateResult.forEach(res -> {
                throw new BusinessRunTimeException(CodeMsg.FAIL.getCode(), res.getMessage());
            });
        }
    }

    /**
     * 手机号码验证方法
     *
     * @return boolean
     * @Date 16:54 2022/7/29
     * @Param [mobile]
     **/
    public static boolean isMobile(String mobile) {
        if (StrUtil.isEmpty(mobile)) {
            return false;
        }
        //匹配原则
        Matcher matcher = PHONE_PATTEN.matcher(mobile);
        return matcher.matches();
    }

    /**
     * 链接地址
     *
     * @return boolean
     * @Date 16:54 2022/7/29
     * @Param [mobile]
     **/
    public static boolean isUrl(String url) {
        if (StrUtil.isEmpty(url)) {
            return false;
        }
        //匹配原则
        Matcher matcher = URL_PATTEN.matcher(url);
        return matcher.matches();
    }

    /**
     * 日期格式校验
     *
     * @param value
     * @param date
     * @return
     */
    public static boolean isDate(String value, String date) {
        if (StrUtil.isEmpty(date)) {
            return false;
        }
        final DateFormat sdf = new SimpleDateFormat(date);
        sdf.setLenient(false);
        try {
            sdf.parse(value);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
